//
//  YYDatePickerView.swift
//  DatePickerView
//
//  Created by 蓓蕾 on 2022/3/22.
//

import UIKit
import SnapKit

class YYDatePickerView: UIView {
    
    let systemBlackColr = UIColor(hex: 0x424243)//系统常用的黑色
    let systemgrayColor = UIColor(hex: 0xb2b2b2)//系统常用的灰色
    
    var CancelReturnBlock: (() -> Void)? //取消按钮的回调
    var SuccessReturnBlock: ((_ date:String) -> Void)?//选择的回调
    
    
    private lazy var titleView = UILabel()
    private lazy var cancelButton = UIButton()
    private lazy var confirmButton = UIButton()
    var lineView = UIView()//一条横线
    var dateFormatter = DateFormatter()
    var datePicker = UIDatePicker()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    
    convenience init() {
        self.init(frame:CGRect.init(x: 0, y: 0, width: 100, height: 100))
        self.setupUI()
    }
    
    func setupUI() {
        
        self.backgroundColor = UIColor.white
        
        titleView.textColor = systemBlackColr
        titleView.textAlignment = .center
        titleView.font = UIFont.systemFont(ofSize: 15)
        titleView.text = "选择时间"
        self.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitleColor(UIColor.system, for: .normal)
        cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        cancelButton.tag = 101
        cancelButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        self.addSubview(cancelButton)
        cancelButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 50, height: 20))
            make.centerY.equalTo(titleView)
        }
        
        lineView.backgroundColor = UIColor.systemGray
        self.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(cancelButton.snp_bottom).offset(15)
        }
        
        confirmButton.setTitle("确定", for: .normal)
        confirmButton.setTitleColor(UIColor.system, for: .normal)
        confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        confirmButton.tag = 102
        confirmButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        self.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 50, height: 20))
            make.centerY.equalTo(titleView)
            make.trailing.equalToSuperview().offset(-15)
        }
        
        dateFormatter.dateFormat = "yyyy-MM-dd"
        datePicker.datePickerMode = .date
        self.addSubview(datePicker)
        datePicker.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(lineView.snp_bottom)
        }
    }
    
    @objc private func buttonClick(btn:UIButton){
        let tag:Int = btn.tag
        switch tag {
        case 101:
            if self.CancelReturnBlock != nil {
                self.CancelReturnBlock!()
            }
        case 102:
            if self.SuccessReturnBlock != nil {
                let currentDate = datePicker.date
                let dateStr = dateFormatter.string(from: currentDate)
                self.SuccessReturnBlock!(dateStr)
            }
        default:
            break
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        fatalError("init(coder:) has not been implemented")
    }
    
//    var title = UILabel.init(lableText: "选择时间")//标题
//    var cancelButton = UIButton.init(title: "取消", bgColor: UIColor.clear, font:  CGFloat(mylableSize)) //取消按钮
//    var confirmButton = UIButton.init(title: "确定", bgColor: UIColor.clear, font:  CGFloat(mylableSize)) //取消按钮
    
    
    
    
    
    
    
    
    
    

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
